package com.demo.house.service.impl;

import com.demo.house.dao.AppointmentDao;
import com.demo.house.dao.HouseSourseDao;
import com.demo.house.dao.UserHouseDao;
import com.demo.house.entity.Appointment;
import com.demo.house.entity.HouseSourse;
import com.demo.house.entity.UserHouse;
import com.demo.house.service.AppointmentService;
import com.demo.house.service.UserHouseService;
import com.demo.house.vo.request.RequestAppointmentVo;
import com.demo.house.vo.response.ResponseAppointmentSelectLandlordVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AppointmentServiceImpl implements AppointmentService {

    @Autowired
    private AppointmentDao appointmentDao;

    @Autowired
    private HouseSourseDao houseSourseDao;

    @Autowired
    private UserHouseDao userHouseDao;

    /**
     * 新增预约
     * @param appointmentVo
     * @return
     */
    @Override
    public int insertSelective(RequestAppointmentVo appointmentVo) {
        Appointment appointment = new Appointment();
        BeanUtils.copyProperties(appointmentVo,appointment);
        appointment.setState(0);
        appointment.setCreateTime(new Date());
        return appointmentDao.insertSelective(appointment);
    }


    /**
     * 租客查询预约
     * @param id
     * @return
     */
    @Override
    public List<Appointment> selectUserId(Integer id) {
        return appointmentDao.selectUserId(id);
    }

    /**
     *  房东查询预约
     * @param id
     * @return
     */
    @Override
    public List<Appointment> selectByUserIdList(Integer id) {
        return appointmentDao.selectByUserIdList(id);
    }


    /**
     * 审核预约
     * @param app
     * @return
     */
    @Override
    public int Review(Appointment app) {
        Appointment appointment = appointmentDao.selectByPrimaryKey(app.getId());
        appointment.setState(1);
        int i = appointmentDao.updateByPrimaryKey(appointment);
        return i;
    }


    @Override
    public void ReviewReservation(Integer id) {
        // 先查出要给哪个人通过预约
        Appointment appointment = appointmentDao.selectByPrimaryKey(id);
        // 查出这个所有预约这个房源的人
        List<Appointment> appointments = appointmentDao.selectByHouseSourceId(appointment.getHouseSourseId());
            for (Appointment appointment1 : appointments) {
                // 先将所有人的预约申请都设置为不同意
                Appointment appointment2 = appointmentDao.selectByPrimaryKey(appointment1.getId());
                appointment2.setState(2);
                appointmentDao.updateStatus(appointment2);
            }
            // 最后再将要同意的这个人的状态修改为1 (同意)
            appointment.setState(1);
            appointmentDao.updateStatus(appointment);

        UserHouse userHouse = new UserHouse();
        userHouse.setHouseSourseId(appointment.getHouseSourseId());
        userHouse.setUserId(id);
        userHouseDao.insert(userHouse);
    }

    @Override
    public void disagreeReservation(Integer id) {
        Appointment appointment = appointmentDao.selectByPrimaryKey(id);
        appointment.setState(2);
        appointmentDao.updateStatus(appointment);
    }
}
